﻿namespace Knownet.Utils
{
  using System;
  using System.Text;
  using System.IO;

  /// <summary>
  /// 代表当前应用程序的根路径。
  /// </summary>
  public class RootPath
  {
    /// <summary>
    /// 用于调试的二进制代码子路径。
    /// </summary>
    public static string DebugBinarySuffix = "\\bin\\debug";
    /// <summary>
    /// 用于最终发布的二进制代码子路径。
    /// </summary>
    public static string ReleaseBinarySuffix = "\\bin\\release";
    /// <summary>
    /// 获取或者设置当前应用程序的根路径。
    /// </summary>
    public static string ApplicationPath
    {
      get
      {
        return Directory.GetCurrentDirectory().ToLower();
      }
      set
      {
        if (value != null)
        {
          Directory.SetCurrentDirectory(value.ToLower());
        }
      }
    }
    /// <summary>
    /// 扩展当前应用程序的根路径，生成
    /// 根路径下子路径的全名。
    /// </summary>
    /// <param name="path">子路径。</param>
    /// <returns>子路径全名。</returns>
    public static string with(string path)
    {
      return Path.Combine(ApplicationPath, path);
    }
    /// <summary>
    /// 使用静态设置，刷新当前根路径。
    /// </summary>
    /// <returns>刷新后的根路径。</returns>
    public static string RefreshRootPath()
    {
      string appdir = ApplicationPath;
      string dbgdir = DebugBinarySuffix.ToLower();
      string reldir = ReleaseBinarySuffix.ToLower();

      if (appdir.EndsWith(dbgdir))
      {
        ApplicationPath = appdir.Remove(appdir.IndexOf(dbgdir));
      }
      else if (appdir.EndsWith(reldir))
      {
        ApplicationPath = appdir.Remove(appdir.IndexOf(reldir));
      }
      return ApplicationPath;
    }
    /// <summary>
    /// 缺省构造，刷新根路径。
    /// </summary>
    static RootPath()
    {
      RefreshRootPath();
    }
  }
}
